package postpc.ex1.ListLayoutsSelector.Widgets;

import postpc.ex1.ListLayoutsSelector.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class FormStuffWidgetActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.form_stuff_layout);

		// For the Button widget
		final Button button = (Button) findViewById(R.id.form_stuff_button);
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// Perform action on clicks
				Log.d("EX1", "Clicked Button");
				Toast.makeText(FormStuffWidgetActivity.this, "Post PC Ex1", Toast.LENGTH_SHORT).show();
			}
		});

		// For the Text widget
		final EditText edittext = (EditText) findViewById(R.id.form_stuff_edittext);
		edittext.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button
				if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
						(keyCode == KeyEvent.KEYCODE_ENTER)) {
					// Perform action on key press
					Toast.makeText(FormStuffWidgetActivity.this, edittext.getText(), Toast.LENGTH_SHORT).show();
					return true;
				}
				return false;
			}
		});

		// For the checkbox widget
		final CheckBox checkbox = (CheckBox) findViewById(R.id.form_stuff_checkbox);
		checkbox.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// Perform action on clicks, depending on whether it's now checked
				if (((CheckBox) v).isChecked()) {
					Log.d("EX1", "checkbox checked");
					Toast.makeText(FormStuffWidgetActivity.this, "Selected", Toast.LENGTH_SHORT).show();
				} else {
					Log.d("EX1", "checkbox NOT checked");
					Toast.makeText(FormStuffWidgetActivity.this, "Not selected", Toast.LENGTH_SHORT).show();
				}
			}
		});

		// For the radio buttons widget
		final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);
		final RadioButton radio_blue = (RadioButton) findViewById(R.id.radio_blue);
		radio_red.setOnClickListener(radio_listener);
		radio_blue.setOnClickListener(radio_listener);

		// For the Toggle Button widget
		final ToggleButton togglebutton = (ToggleButton) findViewById(R.id.togglebutton);
		togglebutton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// Perform action on clicks
				if (togglebutton.isChecked()) {
					Log.d("EX1", "Button checked");
					Toast.makeText(FormStuffWidgetActivity.this, "Checked", Toast.LENGTH_SHORT).show();
				} else {
					Log.d("EX1", "Button NOT checked");
					Toast.makeText(FormStuffWidgetActivity.this, "Not checked", Toast.LENGTH_SHORT).show();
				}
			}
		});

		// For the Rating Bar widget
		final RatingBar ratingbar = (RatingBar) findViewById(R.id.ratingbar);
		ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
			public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
				Log.d("EX1", "Clicked Bar");
				Toast.makeText(FormStuffWidgetActivity.this, "New Rating: " + rating, Toast.LENGTH_SHORT).show();
			}
		});
	}

	// For the radio buttons widget
	private OnClickListener radio_listener = new OnClickListener() {
		public void onClick(View v) {
			// Perform action on clicks
			RadioButton rb = (RadioButton) v;
			Log.d("EX1", "clicked radio");
			Toast.makeText(FormStuffWidgetActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
		}
	};

}
